﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;
using System.Text;

public partial class BaseData_ExpressCompanyEdit : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    private string Code
    {
        get
        {
            if (Request.QueryString["code"] != null)
            {
                return Request.QueryString["code"].ToString();
            }
            else
            {
                return "";
            }
        }
    }
    private Model.TRANER Company
    {
        get
        {
            Model.TRANER company = null;
            if (ViewState["Company"] != null)
            {
                company = (Model.TRANER)ViewState["Company"];
            }
            return company;
        }
        set
        {
            ViewState["Company"] = value;
        }
    }

    private bool IsAdd
    {
        get
        {
            return Convert.ToBoolean(ViewState["IsAdd"]);
        }
        set
        {
            ViewState["IsAdd"] = value;
        }

    }
    private string ExpressBillImageAbsolutePath
    {
        get
        {
            string expressBillImageAbsolutePath = "";
            if (ViewState["ExpressBillImageAbsolutePath"] != null)
            {
                expressBillImageAbsolutePath = ViewState["ExpressBillImageAbsolutePath"].ToString();
            }
            return expressBillImageAbsolutePath;
        }
        set
        {
            ViewState["ExpressBillImageAbsolutePath"] = value;
        }
    }

    private Model.SYSPARAM ExpressImageConfig
    {
        get
        {
            Model.SYSPARAM expressImageConfig = null;
            if (ViewState["ExpressImageConfig"] != null)
            {
                expressImageConfig = (Model.SYSPARAM)ViewState["ExpressImageConfig"];
            }
            return expressImageConfig;
        }
        set
        {
            ViewState["ExpressImageConfig"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("200600103");
        if (!IsPostBack)
        {
            this.ExpressImageConfig = new BLL.SYSPARAM().GetModel("010");
            if (this.ExpressImageConfig == null)
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无法获取系统参数中物流单图片设置信息!');location.href='ExpressCompany.aspx';</script>");
            }

            if (this.Code != "")
            {
                Label1.Text = "编辑物流公司";
                this.Company = new BLL.TRANER().GetModel(this.Code);
                if (this.Company != null)
                {
                    Hid_code.Value = Code;
                    txtCode.Text = this.Company.TRANER_CODE;
                    txtName.Text = this.Company.TRANER_NAME;
                    txtContacter.Text = this.Company.TRANER_CONTACTER;
                    txtPhone.Text = this.Company.TRANER_PHONE1;
                    txtMobile.Text = this.Company.TRANER_PHONE2;
                    URL.Text = Company.TRANER_FIELD2;
                    rbtnUsableNo.Checked = this.Company.TRANER_STOP.ToString() == "0" ? true : false;
                    rbtnUsableYes.Checked = !rbtnUsableNo.Checked;
                    RadioButtonList1.SelectedValue = Company.TRANER_AFTER.ToString();
                    txtNotes.Text = this.Company.TRANER_NOTE;
                    txtSort.Text = this.Company.TRANER_FIELD1;
                    txtCode.Enabled = false;
                    this.IsAdd = false;
                    btnAddOrUpdate.Text = "更新";
                    imgExpressBill.ImageUrl = BLL.Common.GetExpressPicPath() + this.Company.Image;
                }
                else
                {
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('系统中无此记录!');location.href='ExpressCompany.aspx';</script>");
                }
            }
            else
            {
                this.IsAdd = true;
                btnAddOrUpdate.Text = "添加";
            }
        }
        Bind_Standard();
    }
    protected void btnAddOrUpdate_Click(object sender, EventArgs e)
    {
        if (this.IsAdd == true)
        {
            if (Check.CheckPermission("200600103", "00001"))
            {
                if (this.ExpressBillImageAbsolutePath != "")
                {

                    if (txtCode.Text != "")
                    {
                        Model.TRANER company = new BLL.TRANER().GetModel(txtCode.Text.Trim());
                        if (company == null)
                        {
                            company = new Model.TRANER();
                            company.TRANER_CODE = Share.ToDBC(txtCode.Text.Trim());
                            company.TRANER_NAME = txtName.Text.Trim();
                            company.TRANER_CONTACTER = txtContacter.Text.Trim();
                            company.TRANER_PHONE1 = txtPhone.Text.Trim();
                            company.TRANER_PHONE2 = txtMobile.Text.Trim();
                            company.TRANER_FIELD2 = URL.Text.Trim();
                            company.TRANER_AFTER = RadioButtonList1.SelectedValue == "0" ? 0 : 1;
                            company.TRANER_STOP = rbtnUsableYes.Checked ? 1 : 0;
                            company.TRANER_NOTE = txtNotes.Text;
                            company.TRANER_FIELD1 = txtSort.Text.Trim();
                            company.Img_Width = Convert.ToDecimal(Int32.Parse(this.ExpressImageConfig.SP_PARAM19.Split(',')[0]));
                            company.Img_Heigth = Convert.ToDecimal(Int32.Parse(this.ExpressImageConfig.SP_PARAM19.Split(',')[1]));
                            company.Image = this.ExpressBillImageAbsolutePath;
                            if (new BLL.TRANER().Add(company) == company.TRANER_CODE)
                            {
                                Share.InsertLog("200600103", "00001");
                                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('添加记录成功');location.href='ExpressCompany.aspx';</script>");
                            }
                            else
                            {
                                Share.Alert(this.Page, "添加记录失败,请重新添加。");
                            }
                        }
                        else
                        {
                            Share.Alert(this.Page, "已经存在代码为" + txtCode.Text.Trim() + "的记录!");
                        }
                    }
                    else
                    {
                        Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('请输入代码。');</script>");
                    }
                }
                else
                {
                    Share.Alert(this.Page, "请上传物流单图片!");
                }
            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='ExpressCompany.aspx';</script>");
            }
        }
        else
        {
            if (Check.CheckPermission("200600103", "00002"))
            {
                this.Company.TRANER_NAME = txtName.Text.Trim();
                this.Company.TRANER_CONTACTER = txtContacter.Text.Trim();
                this.Company.TRANER_PHONE1 = txtPhone.Text.Trim();
                this.Company.TRANER_PHONE2 = txtMobile.Text.Trim();
                Company.TRANER_FIELD2 = URL.Text.Trim();
                Company.TRANER_AFTER = RadioButtonList1.SelectedValue == "0" ? 0 : 1;
                this.Company.TRANER_STOP = rbtnUsableYes.Checked ? 1 : 0;
                this.Company.TRANER_NOTE = txtNotes.Text;
                this.Company.TRANER_FIELD1 = txtSort.Text.Trim();
                if (this.ExpressBillImageAbsolutePath != "")
                {
                    this.Company.Image = this.ExpressBillImageAbsolutePath;
                }
                if (new BLL.TRANER().Update(this.Company))
                {
                    Share.InsertLog("200600103", "00002");
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('更新记录成功');location.href='ExpressCompany.aspx';</script>");
                }
                else
                {
                    Share.Alert(this.Page,"更新记录失败,请重新编辑更新。");
                }
            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='ExpressCompany.aspx';</script>");
            }

        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ExpressCompany.aspx");
        //Page.RegisterStartupScript("redirect", "<script language=javascript>if(!confirm('要取消" + btnAddOrUpdate.Text + "并返回上一页吗?')) return false; history.go(-1);</script>");  
    }
    private void Bind_Standard()
    {
        Repeater1.DataSource = new BLL.TRANERCOST().GetAllList(Code);
        Repeater1.DataBind();
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            string code = Hid_code.Value;
            string detailid = e.CommandArgument.ToString();
            if (new BLL.TRANERCOST().Delete(code, detailid))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('删除成功!')", true);
                Bind_Standard();
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('删除失败!')", true);
            }
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200300111", "00006"))
        {
            Share.Alert(Page, "无权操作"); return;
        }
        if (File1.Value.Trim() == "")
        {
            imgExpressBill.ImageUrl = "";
            this.ExpressBillImageAbsolutePath = "";
            Share.Alert(Page, "请选择要上传的文件！"); return;
        }

        string filePath = File1.PostedFile.FileName;
        string type = File1.PostedFile.ContentType;
        string filename = DateTime.Now.Ticks.ToString();
        string filenameExtension = filePath.Substring(filePath.LastIndexOf(".")).ToString().Trim();

        
        System.Drawing.Image m_Image = System.Drawing.Image.FromStream(File1.PostedFile.InputStream, true);

        Model.SYSPARAM syspa = new BLL.SYSPARAM().GetModel("001");
        
        if (syspa.SP_PARAM1 == 1)
        {
            string ftpip = syspa.SP_PARAM13;
            string ftpuser = syspa.SP_PARAM14;
            string ftppwd = syspa.SP_PARAM15;
            FtpUpDown fud = new FtpUpDown(ftpip, ftpuser, ftppwd);
            bool result = false;

            filename = DateTime.Now.Ticks.ToString();

            Share.Convert(m_Image, HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/Include/Images/ExpressBillImages/" + filename + filenameExtension), Int32.Parse(this.ExpressImageConfig.SP_PARAM19.Split(',')[0]), Int32.Parse(this.ExpressImageConfig.SP_PARAM19.Split(',')[1]));
            // 把上传的文件写入磁盘

            result = false;
            if (!fud.IsExistsDirectory("", "ExpressBillImages", out result))
            {
                if (result == false)
                {
                    imgExpressBill.ImageUrl = "";
                    this.ExpressBillImageAbsolutePath = "";
                    Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                }
                if (fud.MakeDir("", "ExpressBillImages"))
                {
                    imgExpressBill.ImageUrl = "";
                    this.ExpressBillImageAbsolutePath = "";
                    Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                }
            }
            else
            {
                if (result == false)
                {
                    imgExpressBill.ImageUrl = "";
                    this.ExpressBillImageAbsolutePath = "";
                    Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                }
            }

            if (fud.Upload("ExpressBillImages/", HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/Include/Images/ExpressBillImages/" + filename + filenameExtension), filename + filenameExtension))
            {
                if (File.Exists(HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/Include/Images/ExpressBillImages/" + filename + filenameExtension)))
                {
                    File.Delete(HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/Include/Images/ExpressBillImages/" + filename + filenameExtension));
                }
                imgExpressBill.ImageUrl = BLL.Common.GetExpressPicPath() + filename + filenameExtension;
                this.ExpressBillImageAbsolutePath = filename + filenameExtension;
                Share.Alert(this.Page, "上传成功！");
            }
            else
            {
                if (File.Exists(HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/Include/Images/ExpressBillImages/" + filename + filenameExtension)))
                {
                    File.Delete(HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/Include/Images/ExpressBillImages/" + filename + filenameExtension));
                }
                imgExpressBill.ImageUrl = "";
                this.ExpressBillImageAbsolutePath = "";
                Share.Alert(this.Page, "上传失败");
            }
        }
        else
        {
            Share.Convert(m_Image, HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/Include/Images/ExpressBillImages/" + filename + filenameExtension), Int32.Parse(this.ExpressImageConfig.SP_PARAM19.Split(',')[0]), Int32.Parse(this.ExpressImageConfig.SP_PARAM19.Split(',')[1]));
            if (File.Exists(HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/Include/Images/ExpressBillImages/" + filename + filenameExtension)))
            {
                imgExpressBill.ImageUrl = BLL.Common.GetExpressPicPath() + filename + filenameExtension;
                this.ExpressBillImageAbsolutePath = filename + filenameExtension;
                Share.Alert(this.Page, "上传成功！");
            }
            else
            {
                imgExpressBill.ImageUrl = "";
                this.ExpressBillImageAbsolutePath = "";
                Share.Alert(this.Page, "上传失败");
            }

            
        }
    }
}
